package com.blue.base.oauth.server.config.authentic.filters;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author liulei
 * @version 1.0
 */
@Slf4j
@Component
public class LoginOAuthCheckFilter extends OncePerRequestFilter {
//
//    @Autowired
//    private JdbcClientDetailsService detailsService;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        String clientId = request.getParameter("client_id");
        if (request.getMethod().equals("GET") && request.getServletPath().equals("/login") && !
                StringUtils.isEmpty(clientId)) {
            log.info("check client info...");
//            ClientDetails clientDetails = detailsService.loadClientByClientId(clientId);
//            if (!clientDetails.getClientId().equals(clientId)) {
//                response.sendRedirect("/oauth_error");
//            }
        }
        filterChain.doFilter(request, response);
    }

}
